<?php
namespace Chat\Controller;
use Chat\Controller\ChatController;

/**
 * 企业群,部门群
 */
class EntChatController extends ChatController {
	
	protected function _initialize(){
		parent::_initialize();
	}

	
    /**
     * 根据组织创建聊天群组
     */
    public function createChatByEntAction(){
    	$entid = (int)I('request.entid');
    	$this->_createChatByEnt($entid);
    }
    
    private function _createChatByEnt($entid , $grouptype = 1){
    	$entInfo = $this->_checkEnt($entid);
    	if(!empty($entInfo['chatid'])){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	 
    	$data = array();
    	$data['chatname'] = $entInfo['entname'];
    	$data['grouptype'] = $grouptype;
    	$data['chattype'] = 2;
    	$data['custid'] = $this->_custInfo['custid'];
    	$data['maxcustnum'] = 1000000;
    	$data['status'] = C('DEFAULT_CHAT_STATUS');
    	$chatid = $this->_chat->addChat($data);
    	if($chatid){
    		//取企业下的人员
    		if(empty($this->_ent_cust)) $this->_ent_cust = new \Common\Model\EntCustModel();
    		$custs = $this->_ent_cust->getCustByEnt($entid);
    		if(!empty($custs)){
    			foreach($custs as $cust){
    				$custidsArr[] = $cust['custid'];
    			}
    		}
    		
    		
    		$custidsArr[] = $this->_custInfo['custid'];
    		$custidsArr = array_unique($custidsArr);
    		$custNum = 0;
    		$custids = array();
    		foreach($custidsArr as $custid){
    			if($custNum > (int)$data['maxcustnum'])break;//超出最大数量，就跳过
    	
    			$arr = array();
    			$arr['chatid'] = $chatid;
    			$arr['custid'] = $custid;
    			$arr['status'] = 1;
    			$custInfo = $this->_getCustInfo($custid);//用户不存在，就跳过
    			if(empty($custInfo))continue;
    			$arr['nickname'] = $custInfo['nickname'];
    	
    			$result = $this->_chat_cust->getCust($chatid,$custid);//如果已经是群成员，就跳过
    			if(!empty($result))continue;
    	
    			$ret = $this->_chat_cust->addCust($arr);
    			if($ret){
    				$custNum++;
    				$custids[] = $custid;
    				$this->_delChatListCache($custid);
    			}
    		}
    		 
    		if($custNum == 0){
    			$this->status = 1401;
    			$this->msg = L('STATUS_1401');
    			$this->outPut();
    		}
    		 
    		$this->_chat->custNumInr($chatid , $custNum);
    		$chatInfo = $this->_getChatInfoById($chatid);
    		
    		$arr = array();
    		$arr['entid'] = $entid;
    		$arr['chatid'] = $chatid;
    		$arr['custid'] = $this->_custInfo['custid'];
    		$this->_ent->updateEnt($arr);
    		 
    		$this->data = $chatInfo;
    		$this->outPut();
    	}else{
    		$this->status = 1401;
    		$this->msg = L('STATUS_1401');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 根据组织删除聊天群组
     */
    public function deleteChatByEntAction(){
    	$entid = (int)I('request.entid');
    	$this->_deleteChatByEnt($entid);
    }
    
    private function _deleteChatByEnt($entid){
    	$entInfo = $this->_checkEnt($entid);
    	$this->_deleteChat($entInfo['chatid'] , 0);
    }
    
    
    
    private static function getCustsByDepartId($departid){
    	if(empty($this->_ent_depart)) $this->_ent_depart = new \Common\Model\EntDepartmentModel();
    	$childDeparts = $this->_ent_depart->getDepartByPid($departid);
    	if(!empty($childDeparts)){
    		foreach($childDeparts as $childDepart){
    			$tmpDepartId = $childDepart['departid'];
    			$custidsArr = self::getCustsByDepartId($tmpDepartId);
    		}
    	}
    	
    	if(empty($this->_ent_cust)) $this->_ent_cust = new \Common\Model\EntCustModel();
    	$custs = $this->_ent_cust->getCustByDepart($departid);
    	if(!empty($custs)){
    		foreach($custs as $cust){
    			$custidsArr[] = $cust['custid'];
    		}
    	}
    	
    	return $custidsArr;
    }
    
    
    /**
     * 根据组织部门创建聊天群组
     */
    public function createChatByDepartAction(){
    	$departid = (int)I('request.departid');
    	$this->_createChatByDepart($departid);
    }
    
    private function _createChatByDepart($departid , $grouptype = 1){
    	$departInfo = $this->_checkDepart($departid);
    	if(!empty($departInfo['chatid'])){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    
    	$data = array();
    	$data['chatname'] = $departInfo['departname'];
    	$data['grouptype'] = $grouptype;
    	$data['chattype'] = 2;
    	$data['custid'] = $this->_custInfo['custid'];
    	$data['maxcustnum'] = 1000000;
    	$data['status'] = C('DEFAULT_CHAT_STATUS');
    	$chatid = $this->_chat->addChat($data);
    	if($chatid){
    		//取部门下的人员
    		$custidsArr = self::getCustsByDepartId($departid);
    		
    		$custidsArr[] = $this->_custInfo['custid'];
    		$custidsArr = array_unique($custidsArr);
    		$custNum = 0;
    		$custids = array();
    		foreach($custidsArr as $custid){
    			if($custNum > (int)$data['maxcustnum'])break;//超出最大数量，就跳过
    			 
    			$arr = array();
    			$arr['chatid'] = $chatid;
    			$arr['custid'] = $custid;
    			$arr['status'] = 1;
    			$custInfo = $this->_getCustInfo($custid);//用户不存在，就跳过
    			if(empty($custInfo))continue;
    			$arr['nickname'] = $custInfo['nickname'];
    			 
    			$result = $this->_chat_cust->getCust($chatid,$custid);//如果已经是群成员，就跳过
    			if(!empty($result))continue;
    			 
    			$ret = $this->_chat_cust->addCust($arr);
    			if($ret){
    				$custNum++;
    				$custids[] = $custid;
    				$this->_delChatListCache($custid);
    			}
    		}
    		 
    		if($custNum == 0){
    			$this->status = 1401;
    			$this->msg = L('STATUS_1401');
    			$this->outPut();
    		}
    		 
    		$this->_chat->custNumInr($chatid , $custNum);
    		$chatInfo = $this->_getChatInfoById($chatid);
    		 
    		$arr = array();
    		$arr['departid'] = $departid;
    		$arr['chatid'] = $chatid;
    		$arr['entid'] = $departInfo['entid'];
    		$this->_ent_depart->updateDepart($arr);
    		
    		$this->data = $chatInfo;
    		$this->outPut();
    	}else{
    		$this->status = 1401;
    		$this->msg = L('STATUS_1401');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 根据组织部门删除聊天群组
     */
    public function deleteChatByDepartAction(){
    	$departid = (int)I('request.departid');
    	$this->_deleteChatByDepart($departid);
    }
    
    private function _deleteChatByDepart($departid){
		$departInfo = $this->_checkDepart($departid);
		$this->_deleteChat($departInfo['chatid'] , 0);
    }
    
}
